package com.ccx.library.module.log.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ccx.library.cache.RedisCache;
import com.ccx.library.function.source.SysLog;
import com.ccx.library.module.log.service.LogService;
import com.ccx.library.module.user.entity.User;
import com.ccx.library.response.R;
import com.ccx.library.util.ModelVoIdUtil;
import com.ccx.library.util.WrapperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/log")
public class LogController {
    @Autowired
    private LogService logService;
    @Autowired
    private RedisCache redisCache;
    @GetMapping("/query")
    public R Query(SysLog sysLog){
        return R.SUCCESS(logService.getOne(WrapperUtil.Query(sysLog)));
    }

    @GetMapping
    public R list(){
        if (redisCache.hasKey("log")) {
            Object value = redisCache.getCacheStringKey("log");
            System.out.println(value);
            return R.SUCCESS(value);
        }else{
            List<SysLog> list = logService.list(WrapperUtil.orderDesc(SysLog.class));
            redisCache.setCacheKey("log", list);
            return R.SUCCESS(list);
        }
    }

    @PutMapping
    public R edit(@RequestBody SysLog sysLog){
        return R.SUCCESS(logService.update(WrapperUtil.Update(sysLog, ModelVoIdUtil.getId(sysLog),sysLog.getLogId())));
    }

    @DeleteMapping
    public R DeleteBatch(@RequestBody List<String> ids){
        return R.SUCCESS(logService.removeByIds(ids));
    }

    @GetMapping("/search")
    public R search(SysLog sysLog){
        return R.SUCCESS(logService.list(WrapperUtil.Search(sysLog)));
    }
}
